<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Controllers\utils;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    use utils;

    /*
     * TODO::用户登陆
     * */
    public function login(Request $request){
        $username = $request->input('username','');
        $password = $request->input('password','');
        $username = trim($username);
        $password = trim($password);
        if($username == '' || $password == ''){
            return $this->json_msg(500,'','请输入用户名或密码');
        }else{
            $usableResult = User::usable($username);
            if($usableResult['code'] == 200){
                $credent = [
                    'account'=>$username,
                    'password'=>$password
                ];
                $jwt_token = auth('api')->attempt($credent);
                if(!empty($jwt_token)){
                    return $this->json_msg(200,[
                        'token'=>$jwt_token
                    ],'验证成功');
                }else{
                    return $this->json_msg(500,'','用户名或密码错误');
                }
            }else{
                return $this->json_msg(500,'', $usableResult['msg']);
            }
        }
    }

    public function logout(){
        flush();
        return $this->json_msg(200,'','logout success');
    }

    public function info(Request $request){
        $user = auth('api')->user();
        if($user && isset($user['id'])) {
            if ($user['roles']) {
                $user['roles'] = explode(',', $user['roles']);
            }
            return $this->json_msg(200, $user, 'success');
        }else{
            return $this->json_msg(500,'','登陆超时');
        }
    }

    public function fetchList(Request $request){
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $keyword = $request->input('keyword','');
        $skip = ($page - 1) * $limit;
        $whereRaw = "username like '%{$keyword}%' or account like '%{$keyword}%' or mobile like '%{$keyword}%'";
        $count = DB::table('users')
            ->whereRaw($whereRaw)
            ->count();
        $users = DB::table('users')
            ->whereRaw($whereRaw)
            ->skip($skip)
            ->limit($limit)
            ->select('id','mobile','avatar','username','account','role_id','status','created_at','updated_at')
            ->get();
        return $this->json_msg(200,[
            'users'=>$users,
            'count'=>$count
        ]);
    }

    public function selfSaveInfo(Request $request){
        $form = $request->only('username','mobile','password');
        $form['username'] = isset($form['username'])?trim($form['username']):'';
        $form['mobile'] = isset($form['mobile'])?trim($form['mobile']):'';
        if($form['username'] == ''){
            return $this->json_msg(500,'','请填写用户姓名');
        }
        if($form['password']){
            $form['password'] = bcrypt($form['password']);
        }else{
            unset($form['password']);
        }
        $user = auth('api')->user();
        $uid = $user['id'];
        $form['updated_at'] = date('Y-m-d H:i:s');
        $result = DB::table('users')
            ->where('id',$uid)
            ->update($form);
        if($result){
            return $this->json_msg(200,'','保存成功');
        }else{
            return $this->json_msg(500,'','保存失败');
        }
    }

    public function saveUser(Request $request){
        $form = $request->only('id','account','avatar','username','password','mobile','role_id','status');
        $form['account'] = trim($form['account']);
        $form['username'] = trim($form['username']);
        $form['password'] = isset($form['password'])?trim($form['password']):'';
        $form['mobile'] = trim($form['mobile']);
        $form['status'] = intval($form['status']);
        $form['role_id'] = intval($form['role_id']);
        if($form['account']){
            if(User::accountExist($form['account'],$form['id'])){
                return $this->json_msg(500,'','账号已经存在');
            }
            if($form['id']){
                $form['created_at'] = date('Y-m-d H:i:s');
                if($form['password']){
                    $form['password'] = bcrypt($form['password']);
                }else{
                    unset($form['password']);
                }
                $result = DB::table('users')
                    ->where('id',$form['id'])
                    ->update($form);
            }else{
                if(!$form['password']){
                    return $this->json_msg(500,'','请输入账户密码');
                }
                $form['password'] = bcrypt($form['password']);
                $form['created_at'] = date('Y-m-d H:i:s');
                $form['updated_at'] = date('Y-m-d H:i:s');
                $result = DB::table('users')
                    ->insert($form);
            }
            if($result){
                return $this->json_msg(200,'','保存成功');
            }else{
                return $this->json_msg(500,'','保存失败');
            }
        }else{
            return $this->json_msg(500,'','请输入用户账号');
        }
    }

    /**
     * 获取人的列表
     */
    public function getUsers(){
        $param = \request()->all();
        $page = $param['page'] - 1;
        if ($page < 0){
            $param = 0;
        }

        $count = 30;

        $list = DB::table("user")->where("mobile","<>","")->skip($page * $count)->limit($count)->get();

        $total = DB::table("user")->where("mobile","<>","")->count();

        return $this->json_msg(200,[
            "data"=>$list,
            "total"=>$total
        ],'保存成功');
    }
}
